package com.easy_helth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy_helth.common.R;
import com.easy_helth.entity.Category;
import com.easy_helth.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

//    新增分类
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
//        log.info("category:{}",category);
        return R.success("新增分类成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        log.info("分的页数为:{}",page);
//        分页构造器
        Page<Category> pageinfo = new Page(page,pageSize);
//        条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
//        添加排序条件
        queryWrapper.orderByAsc(Category::getSort);
//        进行分页查询
        categoryService.page(pageinfo,queryWrapper);
        return R.success(pageinfo);
    }

    /**
     * 根据条件
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> listR(Category category){

        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        添加条件
        lambdaQueryWrapper.le(Category::getId,20);
//      添加排序条件
        lambdaQueryWrapper.orderByAsc(Category::getSort).
                orderByAsc(Category::getUpdateTime);
//        返回list
        List<Category> categories = categoryService.list(lambdaQueryWrapper);
        return R.success(categories);
    }

    /**
     * 根据条件
     * @param category
     * @return
     */
    @GetMapping("/listSport")
    public R<List<Category>> listRS(Category category){

        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        添加条件
        lambdaQueryWrapper.gt(Category::getId,20);
//      添加排序条件
        lambdaQueryWrapper.orderByAsc(Category::getSort).
                orderByAsc(Category::getUpdateTime);
//        返回list
        List<Category> categories = categoryService.list(lambdaQueryWrapper);
        return R.success(categories);
    }



    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Integer id){
//        log.info("删除分类:id为:{}",id);
//        categoryService.removeById(id);
        return R.success("删除成功");
    }

    /**
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String > update(@RequestBody Category category){
//        log.info("修改分类{}",category);
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    @GetMapping("getAll")
    public R<List<Category>> getAll(){
        List<Category> categoryList = categoryService.getBaseMapper().selectList(null);
        return R.success(categoryList);
    }

    @GetMapping("/{id}")
    public R<String> getCategoryName(@PathVariable Integer id){
        Category category = categoryService.getById(id);
        return R.success(category.getName());
    }
}
